#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
#=============================================================================
#    Programa para traducir un numero entero de n cifras a palabras en español
#=============================================================================
# LICENCIA:
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see http://www.gnu.org/licenses.
#============================================================================
"""
__file__    = 'trnum.py'
__version__ = '1.0'
__author__  = 'Milton Labanda'
__email__   = '1000ton.lab@gmail.com'
__date__    = '18 Feb 2010'

import sys

# recive a tuple of max 3 items
def trgroup(group):
	units = ['','un','dos','tres','cuatro','cinco','seis','siete','ocho','nueve']
	tensone = ['','once','doce','trece','catorce','quince']
	tens =	['','diez','veinte','treinta','cuarenta','cincuenta','sesenta','setenta','ochenta','noventa']
	hundreds = {1: 'cien', 5: 'quinientos', 7: 'setecientos', 9:'novecientos'}	
	words = str()
	# hundreds translation
	if len(group) == 3:
		hundred = 2
		digit = int(group[hundred])
		if digit == 0:
			pass
		elif digit in (1,5,7,9):
			words += hundreds[digit]
			# if tens is 0
			words += 'to' if digit == 1 and group[1] == '0' else ''
		else:
			words += units[digit] + 'cientos'
	# tens translation
	if len(group) > 1:
		words += ' '
		digit = int(group[1])
		unitemp = int(group[0])
		if digit == 1 and unitemp in (1,2,3,4,5):
			words += tensone[unitemp]
		else:
			words += tens[digit]
	# units translation
	digit = int(group[0])
	if len(group) > 1:
		tentemp = int(group[1])
		if tentemp == 1 and digit in (1,2,3,4,5):
			pass
		else:
			words += ' y ' if digit != 0 and tentemp != 0 else ''
			words += units[digit]	
	else:
		words += units[digit]
	return words
	
def trnumber(s):
	thousands = ['','mil','millones','billones','trillones','cuatrillones','quintillones']
	words = []
	l = list(s) 
	l.reverse()
	groups = [tuple(l[i:i+3]) for i in range(0,len(l),3)]
	m = 0
	for g in groups:
		words.append(' ' + trgroup(g) + ' ' + thousands[m])
		m += 1
	words.reverse()
	return ''.join(words).upper()

if __name__ == "__main__":
	if len(sys.argv) == 2 and sys.argv[1].isdigit(): 
		print trnumber(sys.argv[1])
	else:
		print 'Faltan parametros. User trnum.py [numero]'
"""
Ejemplo de Ejecución:
$ python trnum.py 23235678
 VEINTE Y TRES MILLONES DOSCIENTOS TREINTA Y CINCO MIL SEISCIENTOS SETENTA Y OCHO 
"""
